package com.wzapp.sitposture.usercenter.endpoint;

import com.wzapp.sitposture.authentication.pojo.view.WeXinSign;
import com.wzapp.sitposture.authentication.service.WeXinService;
import com.wzapp.sitposture.common.error.CustomizedException;
import com.wzapp.sitposture.common.util.ToolUtil;
import com.wzapp.sitposture.usercenter.entity.User;
import com.wzapp.sitposture.usercenter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping
public class H5Endpoint {

    @Autowired
    UserService userService;

    @Autowired
    WeXinService weXinService;

    @GetMapping("/h5/share")
    public String register(HttpServletRequest request, @RequestParam(value = "recommendCode", required = false) String recommendCode, Model model) {
        String url = request.getRequestURL().toString();
        System.out.println(url);
        try {
            WeXinSign weXinSign = weXinService.signature(url);
            model.addAttribute("weXinSign", weXinSign);
        } catch (CustomizedException e) {
            e.printStackTrace();
        }
        model.addAttribute("recommendCode", recommendCode);
        User userDB = null;
        if (ToolUtil.isNotEmpty(recommendCode)) {
            userDB = userService.findUserByRecommendCode(recommendCode);
        }
        model.addAttribute("user", userDB);
        return "register";
    }
}
